package com.atwisdom.star.core.dao.service.modelClass.hier.Impl;

import com.atwisdom.star.core.dao.mapper.modelClass.HierInstanceMapper;
import com.atwisdom.star.core.dao.pojo.modelClass.hier.HierInstance;
import com.atwisdom.star.core.dao.service.modelClass.hier.HierInstanceService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author fushb
* @description 针对表【hier_instance】的数据库操作Service实现
* @createDate 2023-06-09 15:23:12
*/
@Service
public class HierInstanceServiceImpl extends ServiceImpl<HierInstanceMapper, HierInstance>
    implements HierInstanceService{

    /**
     * 根据model和className匹配hier数据
     * @param model
     * @param className
     * @return
     */
    @Override
    public List<HierInstance> queryByModelAndClassName(String model, String className) {
        LambdaQueryWrapper<HierInstance> wrapper = buildWrapper(model, className);
        return list(wrapper);
    }

    /**
     * 根据model和className匹配hier数据
     * @param model
     * @param classNameList
     * @return
     */
    @Override
    public List<HierInstance> queryByModelAndClassNameList(String model, List<String> classNameList) {
        LambdaQueryWrapper<HierInstance> wrapper = buildWrapper(model, classNameList);
        return list(wrapper);
    }

    private LambdaQueryWrapper<HierInstance> buildWrapper(String model, String className) {
        LambdaQueryWrapper<HierInstance> wrapper = new LambdaQueryWrapper();
        wrapper.eq(HierInstance::getClassName, className)
                .eq(HierInstance::getContainerModel, model);
        return wrapper;
    }

    private LambdaQueryWrapper<HierInstance> buildRemoveWrapper(String model, String className) {
        LambdaQueryWrapper<HierInstance> wrapper = new LambdaQueryWrapper();
        wrapper.eq(HierInstance::getClassName, className)
                .eq(HierInstance::getContainerModel, model)
                .eq(HierInstance::isB7, false);
        return wrapper;
    }

    private LambdaQueryWrapper<HierInstance> buildWrapper(String model, List<String> classNameList) {
        LambdaQueryWrapper<HierInstance> wrapper = new LambdaQueryWrapper();
        wrapper.in(HierInstance::getClassName, classNameList)
                .eq(HierInstance::getContainerModel, model);
        return wrapper;
    }

    private LambdaQueryWrapper<HierInstance> buildRemoveWrapper(String model, List<String> classNameList) {
        LambdaQueryWrapper<HierInstance> wrapper = new LambdaQueryWrapper();
        wrapper.in(HierInstance::getClassName, classNameList)
                .eq(HierInstance::getContainerModel, model)
                .eq(HierInstance::isB7, false);
        return wrapper;
    }

    private LambdaQueryWrapper<HierInstance> buildRemoveWrapperByLinkClassName(String linkModel, String linkClassName) {
        LambdaQueryWrapper<HierInstance> wrapper = new LambdaQueryWrapper();
        wrapper.eq(HierInstance::getLinkClassName, linkClassName)
                .eq(HierInstance::getLinkModel, linkModel)
                .eq(HierInstance::isB7, false);
        return wrapper;
    }

    private LambdaQueryWrapper<HierInstance> buildRemoveWrapperByLinkClassNameList(String linkModel, List<String> linkClassNameList) {
        LambdaQueryWrapper<HierInstance> wrapper = new LambdaQueryWrapper();
        wrapper.in(HierInstance::getLinkClassName, linkClassNameList)
                .eq(HierInstance::getLinkModel, linkModel)
                .eq(HierInstance::isB7, false);
        return wrapper;
    }

    private LambdaQueryWrapper<HierInstance> buildWrapperByContaierId(String model, Long containerId) {
        LambdaQueryWrapper<HierInstance> wrapper = new LambdaQueryWrapper();
        wrapper.eq(HierInstance::getContainerId, containerId)
                .eq(HierInstance::getContainerModel, model);
        return wrapper;
    }

    private LambdaQueryWrapper<HierInstance> buildWrapperByContaierId(String model, List<Long> containerIdList) {
        LambdaQueryWrapper<HierInstance> wrapper = new LambdaQueryWrapper();
        wrapper.in(HierInstance::getContainerId, containerIdList)
                .eq(HierInstance::getContainerModel, model);
        return wrapper;
    }

    private LambdaQueryWrapper<HierInstance> buildWrapperByLinkId(String linkModel,Long linkId) {
        LambdaQueryWrapper<HierInstance> wrapper = new LambdaQueryWrapper();
        wrapper.eq(HierInstance::getLinkId, linkId).eq(HierInstance::getLinkModel, linkModel);
        return wrapper;
    }

    private LambdaQueryWrapper<HierInstance> buildWrapperByLinkIds(String linkModel,List<Long> linkIdList) {
        LambdaQueryWrapper<HierInstance> wrapper = new LambdaQueryWrapper();
        wrapper.in(HierInstance::getLinkId, linkIdList).eq(HierInstance::getLinkModel, linkModel);
        return wrapper;
    }

    /**
     * 根据model和className删除hier数据
     * @param model
     * @param className
     * @return
     */
    @Override
    public boolean removeByModelAndClassName(String model, String className) {
        LambdaQueryWrapper<HierInstance> wrapper = buildRemoveWrapper(model, className);
        return remove(wrapper);
    }

    /**
     * 根据model和className数组删除hier数据
     * @param model
     * @param classNameList
     * @return
     */
    @Override
    public boolean removeByModelAndClassNameList(String model, List<String> classNameList) {
        LambdaQueryWrapper<HierInstance> wrapper = buildRemoveWrapper(model, classNameList);
        return remove(wrapper);
    }


    /**
     * 根据linkMoel和linkClassName删除hier数据
     * @param linkModel
     * @param linkClassName
     * @return
     */
    @Override
    public boolean removeByLinkModelAndLinkClassName(String linkModel, String linkClassName) {
        LambdaQueryWrapper<HierInstance> wrapper = buildRemoveWrapperByLinkClassName(linkModel, linkClassName);
        return remove(wrapper);
    }

    /**
     * 根据linkMoel和linkClassName数组删除hier数据
     * @param linkModel
     * @param linkClassNameList
     * @return
     */
    @Override
    public boolean removeByLinkModelAndLinkClassNameList(String linkModel, List<String> linkClassNameList) {
        LambdaQueryWrapper<HierInstance> wrapper = buildRemoveWrapperByLinkClassNameList(linkModel, linkClassNameList);
        return remove(wrapper);
    }

    @Override
    public boolean removeByModelAndContainerIdList(String model, List<Long> containerIdList) {
        LambdaQueryWrapper<HierInstance> wrapper = buildWrapperByContaierId(model, containerIdList);
        return remove(wrapper);
    }

    @Override
    public boolean removeByModelAndContainerId(String model, Long containerId) {
        LambdaQueryWrapper<HierInstance> wrapper = buildWrapperByContaierId(model, containerId);
        return remove(wrapper);
    }

    /**
     * 根据linkModel和关联id的数组删除hier数据
     * @param linkIdList 关联id的数组信息
     * @return
     */
    @Override
    public boolean removeByLinkModelAndLinkIdList(String linkModel, List<Long> linkIdList) {
        LambdaQueryWrapper<HierInstance> wrapper = buildWrapperByLinkIds(linkModel, linkIdList);
        return remove(wrapper);
    }

    /**
     * 根据linkModel和关联id删除hier数据
     * @param linkId 关联id的信息
     * @return
     */
    @Override
    public boolean removeByLinkModelAndLinkId(String linkModel, Long linkId) {
        LambdaQueryWrapper<HierInstance> wrapper = buildWrapperByLinkId(linkModel, linkId);
        return remove(wrapper);
    }

    @Override
    public boolean removeByParentId(String model, Long containerId, String parentNodeId) {
        LambdaQueryWrapper<HierInstance> wrapper = buildWrapperByContaierId(model, containerId);
        wrapper.eq(HierInstance::getParentId, parentNodeId);
        return remove(wrapper);
    }
}




